#include "Logger.h"
#include <sstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

//======================================================
// Logging utility
//======================================================

Logger::Logger(const string & outputDir /* = "" */)
: m_enabled(false)
{
	if (outputDir.length() > 0)
	{
		setPath(outputDir);
	}
}

bool Logger::setPath(const string & outputDir)
{
	m_logFilePath = outputDir + "\\popup.log";
	FILE *fd = fopen(m_logFilePath.c_str(), "w+");
	m_enabled = (fd != NULL);
	fclose(fd);
	return m_enabled;
}



void Logger::flush()
{
	if (m_enabled)
	{
		FILE *fd = fopen(m_logFilePath.c_str(), "a+");

		if (fd != NULL)
		{
			fprintf(fd, "%s\n", this->str().c_str());
			this->str("");
		}
		fclose(fd);
	}
}
